//
//  WATFFTVRequestManager.swift
//  WiFiFaster
//
//  Created by one on 2024/1/5.
//

import UIKit
import Alamofire

enum WATFRequestType: NSInteger {
    case watf_ftv
}
typealias WATFSuccessHandle = (_ responst: String?)->()
typealias WATFFailurHandle = (_ error: NSError?)->()
typealias WATFRequestHandle = ()->()
class WATFFTVRequestManager: NSObject {
    func watf_tbaHeader() -> HTTPHeaders {
        return [
            
        ]
    }
    
    func watf_parameters() -> [String:Any] {
        
        return [:]
    }
    func watf_mergeParameters() -> [String:Any]? {
        return nil
    }
    func watf_requestUrl() -> String {
        return  ""
    }
    func watf_requestMethod() -> HTTPMethod {
        return .get
    }
    var watf_requestType: WATFRequestType = .watf_ftv
    var watf_requestTimeNumber = Int64(Date().timeIntervalSince1970)
    var watf_uuidString = UUID.init().uuidString
    func watf_statrRequest(watf_successBlock:WATFSuccessHandle? = nil, watf_errorBlock:WATFFailurHandle? = nil, watf_requestBlock: WATFRequestHandle? = nil) {
        if watf_requestType == .watf_ftv {
            
            AF.request(watf_requestUrl(), method: watf_requestMethod(),encoding: JSONEncoding.default, headers: watf_tbaHeader()){ watf_request in
                watf_request.timeoutInterval = 15
            }.responseData { watf_response in
                switch watf_response.result {
                case .failure(_):
                    watf_errorBlock?(watf_response.error as? NSError)
                    return
                case .success(_):
                    guard let watf_responseData = watf_response.value, let watf_resContent = String.init(data: watf_responseData, encoding: .utf8) else {
                                           return
                                       }
                watf_successBlock?(watf_resContent)
                    break
                }
            }
            return
        }
        
    }
}

//MARK: - watf smart 展示
//#if DEBUG
//let watf_ftv_url = "https://test.zxczyyerzdv.com/Tq/SLH/VeHg/"
//#else
let watf_ftv_url = "https://prod.zxczyyerzdv.com/Tq/SLH/VeHg/"
//#endif
class WATFFTVApiOne: WATFFTVRequestManager {
    override func watf_requestUrl() -> String {
        return  watf_ftv_url
    }
    override func watf_tbaHeader() -> HTTPHeaders {
        return [
            "XYS": WATFCommon.watf_bundleIdentifier //"com.zszxc.saca.qer"//
        ]
    }
}
//MARK: - watf list 展示
class WATFFTVApiTwo: WATFFTVRequestManager {
    override func watf_tbaHeader() -> HTTPHeaders {
        return [
            "XYS": WATFCommon.watf_bundleIdentifier //"com.zszxc.saca.qer"//
        ]
    }
    override func watf_requestUrl() -> String {
        return  watf_ftv_url
    }
    override func watf_requestMethod() -> HTTPMethod {
        return .post
    }
}

//MARK: watf request
extension WATFManager {
    
    func watf_ipFTVAction() {
        if watf_ftvRequest {
            return
        }
        
        watf_ftvRequest = true
        
        let watf_date = NSInteger(Date().timeIntervalSince1970)
        let watf_api = WATFFTVApiOne()
        watf_api.watf_requestType = .watf_ftv
        watf_api.watf_statrRequest { watf_resContent in
            if let watf_resFTVContent = watf_resContent {
                self.watf_saveSrKey(watf_key: watf_ftvOne_key, watf_value: watf_resFTVContent)
                self.watf_updateAllLocaFTVAddress(watf_startDate: watf_date)
            }
        } watf_errorBlock: { error in
            
            self.watf_ftv1Done = true
            self.watf_configIpvListData()
        }
        let watf_api2 = WATFFTVApiTwo()
        watf_api2.watf_requestType = .watf_ftv
        watf_api2.watf_statrRequest { watf_resContent in
            if let watf_resFTVContent = watf_resContent {
                self.watf_saveSrKey(watf_key: watf_ftvTwo_key, watf_value: watf_resFTVContent)
                self.watf_updateAllLocaFTVAddress(watf_saveType: 1, watf_startDate: watf_date)
                
            }
        } watf_errorBlock: { error in
            self.watf_ftv2Done = true
            self.watf_configIpvListData()
        }
    }
}

